package com.heima.xxljob.task;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@Slf4j
public class DemoTask {

    @XxlJob("demoJobHandler")
    public void simpileTask(){
        log.info("demoJobHandler 执行了");
    }

    /**
     * 分片广播的任务
     */

    @XxlJob("sharding")
    public void shardingTask(){
        //1. 构建10000个任务
        List<Integer> tasks = getData();

        //2. 获取自己的分片以及总分片数量
        int shardIndex = XxlJobHelper.getShardIndex(); //自己消费几号分配
        int shardTotal = XxlJobHelper.getShardTotal(); //总共拥有多少个分配

        //3. 自定义分配规则并消费任务
        for (int i=0; i<tasks.size(); i++){
            //自定义分配规则
            if(i % shardTotal == shardIndex){
                System.out.println(tasks.get(i));
            }
        }
    }

    private List<Integer> getData() {
        List<Integer> list = new ArrayList<>(10000);
        for (int i=1; i <= 10000; i++){
            list.add(i);
        }
        return list;
    }
}
